using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace UIDCToolsProfiler
{
    public class UIDCToolWindow : EditorWindow
    {
        private int defaultDepth = UIDCTools.DefaultDepth;
        private int depthIdx = 1;
        private GameObject m_selectObj = null;
        private UIDCTreeNode m_treeRootNode = null;
        void OnGUI()
        {
            GUILayout.Space(5);
            CreateSplit();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Catch"))
            {
                Catch();
            }
            if (GUILayout.Button("Refresh"))
            {
                Refresh();
            }
            if (GUILayout.Button("Clear"))
            {
                Reset();
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(2);
            CreateSplit();

            ShowCatchUI();
        }
        private Vector2 scrollPos = Vector2.zero;
        private void ShowCatchUI()
        {
            if (m_selectObj != null)
            {
                GUILayout.Space(2);
                if(m_treeRootNode != null)
                {
                    GUILayout.Label(string.Format("Batches: {0}  MaskBatches: {1}", m_treeRootNode.batchCount, m_treeRootNode.maskBatchCount));
                    GUILayout.Space(2);
                    scrollPos = EditorGUILayout.BeginScrollView(scrollPos, true, true, GUILayout.Width(1000), GUILayout.Height(600));
                    m_treeRootNode.OnGUI();
                    EditorGUILayout.EndScrollView();
                }
            }
        }

        private void Catch()
        {
            if (Selection.activeGameObject == null)
            {
                EditorUtility.DisplayDialog("WARNING", "Please Select a Object ！", "OK");
                return;
            }

            if (Selection.activeGameObject.layer == LayerMask.NameToLayer("UI"))
            {
                m_selectObj = Selection.activeGameObject;
                Refresh();
            }
        }

        private void Refresh()
        {
            if (m_treeRootNode != null)
            {
                m_treeRootNode.Destroy();
            }

            if (m_selectObj != null)
            {
                depthIdx = 0;
                UIDCTreeNode.BatchesList.Clear();
                UIDCTools.ClearTexture();
                m_treeRootNode = new UIDCTreeNode(m_selectObj.name, m_selectObj, depthIdx * defaultDepth);
                m_treeRootNode.IsRoot = true;
                m_treeRootNode.isCanvas = true;
                GenChildNodes(m_treeRootNode, m_selectObj.transform);
                UIDCTools.GenTreeInfo(m_treeRootNode);
            }
        }

        private void GenChildNodes(UIDCTreeNode node, Transform transform)
        {
            if (transform.childCount > 0)
            {
                int depth = 0;
                for (int i = 0; i < transform.childCount; ++i)
                {
                    Transform child = transform.GetChild(i);
                    if (child.gameObject.activeSelf)
                    {
                        MaskableGraphic ui = child.GetComponent<MaskableGraphic>();
                        if(ui != null && ui.mainTexture == null)
                        {
                            continue;
                        }
                        depth = node.Depth + 1;
                        bool isCanvas = false;
                        if (child.GetComponent<Canvas>() != null)
                        {
                            isCanvas = true;
                            ++depthIdx;
                            depth = depthIdx * defaultDepth;
                        }
                        UIDCTreeNode childNode = new UIDCTreeNode(child.name, child.gameObject, depth);
                        childNode.isCanvas = isCanvas;
                        GenChildNodes(childNode, child);
                        node.AddChild(childNode);
                    }
                }
            }
        }

        private void Reset()
        {
            m_selectObj = null;
            m_treeRootNode = null;
        }

        public void CreateSplit()
        {
            GUILayout.Label("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        }
    }
}
